package com.lianzhihui.romeex.ui.account;

import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.adapter.WalletManagerAdapter;
import com.lianzhihui.romeex.base.AppConfig;
import com.lianzhihui.romeex.bean.user.WalletInfo;
import com.lianzhihui.romeex.config.DataManager;
import com.lianzhihui.romeex.config.MessageEvent;
import com.lianzhihui.romeex.mvp.presenter.SyncUserPresenter;
import com.lianzhihui.romeex.mvp.view.SyncUserView;
import com.lianzhihui.romeex.ui.MainActivity;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;
import com.lianzhihui.romeex.utils.UserUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.lianzhihui.romeex.config.AppConstants.Event.REFRUSH_WALLET;

/**
 * Description:
 * Author: RAMON
 * CreateDate: 2020/10/30 3:26 PM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class WalletManagerActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener, SyncUserView {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.bt_create)
    Button bt_create;
    @BindView(R.id.bt_import)
    Button bt_import;
    List<WalletInfo> wallets;
    WalletManagerAdapter adapter;

    SyncUserPresenter presenter;
    String address;
    WalletInfo walletInfo;
    boolean isCheck = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wallet_manager_layout;
    }

    @Override
    protected void initView() {
        setTitle(getString(R.string.wallet_manager));
        presenter = new SyncUserPresenter(this, this);
        isCheck = getIntent().getBooleanExtra("isCheck", false);
        //获取 钱包列表
        wallets = UserUtils.getAllUserList();
        //加载钱包列表
        loadData();
    }

    private void loadData() {

        if (adapter == null) {
            adapter = new WalletManagerAdapter(this, wallets);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(adapter);
            adapter.setEmptyView(getEmptyView());
            adapter.setOnItemClickListener(this);
            adapter.setOnItemChildClickListener(this);
        } else {
            adapter.setNewData(wallets);
        }
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.bt_create, R.id.bt_import})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_create:
                startActivity(new Intent(this, SetNameSaftyActivity.class)
                        .putExtra("isManager", true));
                break;
            case R.id.bt_import:
                startActivity(new Intent(this, ImportWalletActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        showLoading(getString(R.string.syncing));
        walletInfo = (WalletInfo) adapter.getItem(position);
//        LogUtil.error("ramon", item.getAddress() + "");
        presenter.syncUser(walletInfo.getAddress());
        address = walletInfo.getAddress();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            switch (view.getId()) {
                case R.id.item_tv_wallet_addr:
                case R.id.item_tv_wallet_layout:
                    WalletInfo wallet = (WalletInfo) adapter.getItem(position);
                    startActivity(new Intent(this, AddressQRCodeActivity.class).putExtra(AppConfig.ADDRESS, wallet.getAddress()));
                    break;
                case R.id.item_iv_setting:
                    Log.e("ramon", "setting 点击");
                    WalletInfo wallet2 = (WalletInfo) adapter.getItem(position);
                    startActivity(new Intent(this, WalletSettingActivity.class)
                            .putExtra(AppConfig.ADDRESS, wallet2.getAddress())
                            .putExtra(AppConfig.IS_LAST, adapter.getItemCount() < 2));
                    break;
                default:
                    break;
            }
        }

    }

    @Override
    public void onMessageEvent(MessageEvent event) {
        super.onMessageEvent(event);
        if (event.getCode() == REFRUSH_WALLET) {
            //获取 钱包列表
            wallets = UserUtils.getAllUserList();
            //加载钱包列表
            loadData();
        }
    }

    @Override
    public void onSync() {

        UserUtils.setCurrentUserAddress(walletInfo.getAddress());
        DataManager.setUserInfo(walletInfo);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!isCheck) {
                    MainActivity.instance.finish();
                    startActivity(new Intent(WalletManagerActivity.this, MainActivity.class));
                }
                dismissLoading();
                finish();
            }
        }, 1000);
    }

    @Override
    public void onSyncFail(Object obj) {
        dismissLoading();
    }
}
